import java.io.*;
import java.util.*;

/**
 * @version 1.12 2007-06-22
 * @author Cay Horstmann
 */
public class TextFileTest
{
   public static void main(String[] args)
   {
     Student[] staff = new Student[3];

      staff[0] = new Student(1,"Zhang",18,97);
      staff[1] = new Student(2, "Li", 16, 85.0);
      staff[2] = new Student(3,"Wang",17,76);

      try
      {
         // save all Student records to the file Student.dat
         PrintWriter out = new PrintWriter("d:/Student.txt");
         writeData(staff, out);
         out.close();

         // retrieve all records into a new array
         Scanner in = new Scanner(new FileReader("d:/Student.txt"));
         Student[] newStaff = readData(in);
         in.close();

         // print the newly read Student records
         for (Student e : newStaff)
            System.out.println(e);
      }
      catch (IOException exception)
      {
         exception.printStackTrace();
      }
   }

   /**
    * Writes all Students in an array to a print writer
    * @param Students an array of Students
    * @param out a print writer
    */
   private static void writeData(Student[] Students, PrintWriter out) throws IOException
   {
      // write number of Students
      out.println(Students.length);

      for (Student e : Students)
         e.writeData(out);
   }

   /**
    * Reads an array of Students from a scanner
    * @param in the scanner
    * @return the array of Students
    */
   private static Student[] readData(Scanner in)
   {
      // retrieve the array size
      int n = in.nextInt();
      in.nextLine(); // consume newline

      Student[] Students = new Student[n];
      for (int i = 0; i < n; i++)
      {
         Students[i] = new Student();
         Students[i].readData(in);
      }
      return Students;
   }
}

 class Student {
	private int id;
	private String name;
	private int age;
	private double grade;
	
	public Student(){
		
	}
	public Student(int id, String name, int age, double grade) {
		this.id = id;
		this.setName(name);
		this.setAge(age);
		this.setGrade(grade);
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if (name.length()>10){
			throw new IllegalArgumentException("name's length should <=10 "+name.length());
		}
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if (age<=0){
			throw new IllegalArgumentException("age should >0 "+age);
		}
		this.age = age;
	}
	public double getGrade() {
		return grade;
	}
	public void setGrade(double grade) {
		if (grade<0 || grade >100){
			throw new IllegalArgumentException("grade should be in [0,100] "+grade);
		}
		this.grade = grade;
	}
	
	 @Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
	}
	public void writeData(PrintWriter out)
	   {
	      
	      out.println(id + "|" + name + "|" + age + "|"
	            + grade);
	   }

	   /**
	    * Reads Student data from a buffered reader
	    * @param in the scanner
	    */
	   public void readData(Scanner in)
	   {
	      String line = in.nextLine();
	      String[] tokens = line.split("\\|");
	      id = Integer.parseInt(tokens[0]);
	      name=tokens[1];
	      age=Integer.parseInt(tokens[2]);
	      grade=Double.parseDouble(tokens[3]);
	     
	   }

 
	
}
